#include "CommonUtils.h"
const QTime CommonUtils::TIME_MIDNIGHT= QTime::fromString("0000", "hhmm");
const string CommonUtils::STRING_COMMAND_ADD= "add";
const string CommonUtils::STRING_COMMAND_DELETE="delete";
const string CommonUtils::STRING_COMMAND_SET_DONE="done";
const string CommonUtils::STRING_COMMAND_EDIT="edit";
const string CommonUtils::STRING_COMMAND_DISPLAY="display";
const string CommonUtils::STRING_COMMAND_DISPLAY_ALL="displayall";
const string CommonUtils::STRING_COMMAND_SORT="sort";
const string CommonUtils::STRING_COMMAND_SEARCH="search";
const string CommonUtils::STRING_COMMAND_UNDO="undo";
const string CommonUtils::STRING_COMMAND_REDO="redo";
const string CommonUtils::STRING_COMMAND_DISPLAY_TASK="displayt";
const string CommonUtils::STRING_COMMAND_DISPLAY_EVENT="displaye";
const string CommonUtils::STRING_COMMAND_DISPLAY_WEEK="displayw";
const string CommonUtils::STRING_COMMAND_DISPLAY_DAY="displayd";
const string CommonUtils::STRING_COMMAND_DISPLAY_DAY_BEFORE="displaydb";
const string CommonUtils::STRING_COMMAND_DISPLAY_PAST_NOT_DONE="displaydnotdone";
const string CommonUtils::STRING_COMMAND_CLEAR="clear";
const string CommonUtils::STRING_COMMAND_DISPLAY_DONE="displaydone";
const string CommonUtils::STRING_COMMAND_DISPLAY_NOT_DONE="displaynotdone";
const string CommonUtils::STRING_COMMAND_EXIT="exit";
const string CommonUtils::ENTRY_TYPE_EVENT= "EVENT";
const string CommonUtils::ENTRY_TYPE_TASK= "TASK";
const QString CommonUtils::FORMAT_DATE="dd/MM/yyyy";
const QString CommonUtils::FORMAT_TIME="hhmm";

CommonUtils::CommonUtils(void)
{
}


CommonUtils::~CommonUtils(void)
{
}


string CommonUtils::toLower(string input) {
	for(int i=0; i<input.length(); input[i]=tolower(input[i]), i++);
	return input;
}

bool CommonUtils::greater(const Entry* a, const Entry* b)
{
	QDate aDate = a->getStartDate();
	QTime aTime = a->getStartTime();
	if(!aDate.isValid() || !aTime.isValid()) {
		aDate = a->getEndDate();
		aTime = a->getEndTime();
	}
	QDate bDate = b->getStartDate();
	QTime bTime = b->getStartTime();
	if(!bDate.isValid() || !bTime.isValid()) {
		bDate = b->getEndDate();
		bTime = b->getEndTime();
	}
	if (QDateTime(aDate,aTime) == QDateTime(bDate, bTime))
		return a->getName() < b->getName();

	return QDateTime(aDate,aTime) < QDateTime(bDate,bTime);
}
Entry* CommonUtils::compareEntry(Entry* initialEntry, Entry* newEntry) {
	if((initialEntry->getEntryType()) != (initialEntry->getEntryType())){
		return CommonUtils::compareByType(initialEntry, newEntry);
	}
	else{
		if(((initialEntry->getEntryType()) == newEntry->getEntryType()) && (initialEntry->getEntryType() == "EVENT")){
			if(QDate(initialEntry->getStartDate()) != QDate(newEntry->getStartDate())){
				return CommonUtils::compareByStartDate(initialEntry, newEntry);
			}
			else{
				if(QTime(initialEntry->getStartTime()) != QTime(newEntry->getStartTime())){
					return CommonUtils::compareByStartTime(initialEntry, newEntry);
				}
				else{
					if((initialEntry->getName()) != (newEntry->getName())){
							return CommonUtils::compareByName(initialEntry, newEntry);
					}
					else{
						if(QTime(initialEntry->getEndTime()) != QTime(newEntry->getEndTime())){
						return CommonUtils::compareByEndTime(initialEntry, newEntry);
						}
						else{
							if((initialEntry->getStatus()) != (newEntry->getStatus())){
							return CommonUtils::compareByStatus(initialEntry, newEntry);
							}
						}
					}
				}
			}
		//return new Entry();
		}
		if(((initialEntry->getEntryType()) == newEntry->getEntryType()) && (initialEntry->getEntryType() == "TASK")){
			if((initialEntry->getStatus()) != (newEntry->getStatus())){
							return CommonUtils::compareByStatus(initialEntry, newEntry);
			}
			else{
				if(QDateTime(initialEntry->getEndDate(), initialEntry->getEndTime()) != QDateTime(newEntry->getEndDate(), newEntry->getEndTime())){
					return CommonUtils::compareByEndDateTime(initialEntry, newEntry);
				}
				else{
					if((initialEntry->getName()) != (newEntry->getName())){
						return CommonUtils::compareByName(initialEntry, newEntry);
					}
				}
			}
		}

	}
//return new Entry();
}
Entry* CommonUtils::compareByType(Entry* initialEntry, Entry* newEntry){
	if((initialEntry->getEntryType()) < (newEntry->getEntryType())){
		return initialEntry;
	}
	else{
		return newEntry;
	}
}
Entry* CommonUtils::compareByStartDate(Entry* initialEntry, Entry* newEntry){
	if(QDate(initialEntry->getStartDate()) < QDate(newEntry->getStartDate())){
		return initialEntry;
	}
	else{
		return newEntry;
	}
;}
Entry* CommonUtils::compareByStartTime(Entry* initialEntry, Entry* newEntry){
	if(QTime(initialEntry->getStartTime()) < QTime(newEntry->getStartTime())){
		return initialEntry;
	}
	else{
		return newEntry;
	}
;}
Entry* CommonUtils::compareByEndTime(Entry* initialEntry, Entry* newEntry){
	if(QTime(initialEntry->getEndTime()) < QTime(newEntry->getEndTime())){
		return initialEntry;
	}
	else{
		return newEntry;
	}
;}
Entry* CommonUtils::compareByName(Entry* initialEntry, Entry* newEntry){
	if((initialEntry->getName()) < (newEntry->getName())){
		return initialEntry;}
	else{
		return newEntry;
	}
;}
Entry* CommonUtils::compareByStatus(Entry* initialEntry, Entry* newEntry){
	if((initialEntry->getStatus()) < (newEntry->getStatus())){
		return initialEntry;
	}
	else{
		return newEntry;
	}
;}
Entry* CommonUtils::compareByEndDateTime(Entry* initialEntry, Entry* newEntry){
	if(QDateTime(initialEntry->getEndDate(), initialEntry->getEndTime()) < QDateTime(newEntry->getEndDate(), newEntry->getEndTime())){
		return initialEntry;
	}
	else{
		return newEntry;
	}
}
const string CommonUtils::_fileName = "info.log";